/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 5/7/14
 * Time: 8:41 AM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.codeAlong.state.codeExercise.view
{
import flash.events.Event;
import flash.events.FocusEvent;
import flash.text.TextField;

public class DefaultTextField extends TextField
{
  private var _textField:TextField;

  public function DefaultTextField()
  {
    this.mouseEnabled = false;
  }

  public function set textField(value:TextField):void
  {
    if (_textField != null)
    {
      _textField.removeEventListener(FocusEvent.FOCUS_IN, onTextFieldFocusIn);
      _textField.removeEventListener(FocusEvent.FOCUS_OUT, onTextFieldFocusOut);
    }
    _textField = value;
    if (_textField != null)
    {
      onTextFieldFocusOut(null); //Refresh it once.
      _textField.addEventListener(FocusEvent.FOCUS_IN, onTextFieldFocusIn);
      _textField.addEventListener(FocusEvent.FOCUS_OUT, onTextFieldFocusOut);
    }
  }

  private function onTextFieldFocusIn(event:Event):void
  {
    this.visible = false;
  }

  private function onTextFieldFocusOut(event:Event):void
  {
    refresh();
  }

  public function refresh():void
  {
    this.visible = _textField && _textField.text.length == 0;
  }
}
}
